//
//  LZSegmentControl.m
//  benlaijishi_shop
//
//  Created by 梁泽 on 16/2/25.
//  Copyright © 2016年 iNeverGiveUp. All rights reserved.
//

#import "LZSegmentControl.h"
#import "Masonry.h"
#define kMargin 15
#define kSegmentHeight 35
@interface LZSegmentControl()
@property (nonatomic,strong) NSArray *titleArray;
@property (nonatomic,weak)  UIView *lineView;
//@property (nonatomic,strong) UIButton *selectBtn;
@property (nonatomic,strong) UIButton *lastBtn;

@end

@implementation LZSegmentControl
- (instancetype)initWithFrame:(CGRect)frame Items:(NSArray *)titleItem{
    self = [super initWithFrame:frame];
    _titleArray = titleItem;
    [self setupUI];
    return self;
}
- (void) setupUI {
    self.backgroundColor = UIColorFromRGB(0xededed);
    UIView *back = [[UIView alloc] init];
    [self addSubview:back];
    back.backgroundColor = [UIColor whiteColor];
    [back mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(self);
        make.height.equalTo(@(kSegmentHeight));
    }];
    
    CGFloat width = (kScreenWidth-2*kMargin)/self.titleArray.count;
    [_titleArray enumerateObjectsUsingBlock:^(NSString *title, NSUInteger idx, BOOL *stop) {
        CGFloat height = kSegmentHeight;
        CGFloat x = kMargin + idx*width;
        UIButton *btn = [[UIButton alloc] initWithFrame:CGRectMake(x, 0, width, height)];
        btn.titleLabel.font = [UIFont systemFontOfSize:14];
        [btn setTitle:title forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor grayColor] forState:UIControlStateNormal];
        [btn setTitleColor:kThemeColor forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        btn.tag = 100 + idx;
        [self addSubview:btn];
    }];
    
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(kMargin, kSegmentHeight-2, width, 2)];
    lineView.backgroundColor = kThemeColor;
    [self addSubview:_lineView = lineView];
}
- (void) setSelectSegmentIndex:(NSInteger)selectSegmentIndex{
    UIButton *btn = [self viewWithTag:100 + selectSegmentIndex];
    [self clickBtn:btn];
}
- (void) clickBtn:(UIButton*)sender{
    if (self.lastBtn == sender) {
        return ;
    }
    self.lastBtn.selected = NO;
    sender.selected = YES;
    self.lastBtn = sender;
    NSInteger index = sender.tag - 100;
    CGFloat width = (kScreenWidth-2*kMargin)/self.titleArray.count;
    [UIView animateWithDuration:0.3 animations:^{
        self.lineView.transform = CGAffineTransformMakeTranslation(index*width, 0);
    }];
    if (_valueChange) {
        _valueChange(index);
    }
}

- (void) moveIndexWithProgress:(CGFloat)progress{
    CGFloat width = (kScreenWidth-2*kMargin)/self.titleArray.count;
    self.lineView.transform = CGAffineTransformMakeTranslation(progress*width, 0);
    NSInteger index = progress + 0.5;
    UIButton *btn = [self viewWithTag:100 + index];
    self.lastBtn.selected = NO;
    btn.selected = YES;
    self.lastBtn = btn;
}
@end
